package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.aopLog.OperateLog;
import com.huilian.iotbox.admin.wap.service.CommodityService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.CommodityDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.po.Commodity;
import com.huilian.iotbox.data.vo.CommodityVo;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/commodity")
public class CommodityController extends UserInfoController {
    @Autowired
    private CommodityService commodityService;

    /**
     * 查询游戏币套餐
     */
    @GetMapping("/findByStoreIdAndUserIdCommodity")
    public ServerResponse findByStoreIdAndUserId(Integer storeId, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        CommodityDto commodityDto = new CommodityDto();
        if (userInfo.getRoleId().compareTo(RoleEnum.SUB_ACCOUNT.getRoleId()) == 0) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }
        commodityDto.setUserInfo(userInfo);
        commodityDto.setMerchantStoreId(storeId);
        return ServerResponse.createBySuccess(commodityService.findByStoreIdAndUserIdCommodity(commodityDto));
    }
    /**
     * 查询余额充值套餐
     */
    @GetMapping("/findByStoreIdAndUserIdCommodityV2")
    public ServerResponse findByStoreIdAndUserIdCommodityV2(Integer storeId, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        CommodityDto commodityDto = new CommodityDto();
        if (userInfo.getRoleId().compareTo(RoleEnum.SUB_ACCOUNT.getRoleId()) == 0) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }
        commodityDto.setUserInfo(userInfo);
        commodityDto.setMerchantStoreId(storeId);
        return ServerResponse.createBySuccess(commodityService.findByStoreIdAndUserIdCommodityV2(commodityDto));
    }


    /**
     * 删除余额套餐
     *
     * @param request
     * @param commodityDto
     * @return
     */
    @OperateLog
    @PutMapping("/deleteBalancePackage")
    public ServerResponse deleteBalancePackage(@RequestBody CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage("获取商户信息失败");
        }
        if (userInfo.getRoleId().compareTo(RoleEnum.SUB_ACCOUNT.getRoleId()) == 0) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }
        commodityDto.setUserInfo(userInfo);
        return commodityService.deleteBalancePackage(commodityDto);
    }
    @OperateLog
    @PutMapping("/editPackage")
    public ServerResponse editPackage(@RequestBody CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage("获取商户信息失败");
        }
        commodityDto.setUserInfo(userInfo);
        return commodityService.editPackage(commodityDto);
    }


    /**
     * 商品按上架 下架查询
     */
    @PostMapping("/findByStatus")
    public ServerResponse findByStatus(@RequestBody CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityDto.setUserInfo(userInfo);
        return commodityService.findByStatus(commodityDto);
    }

    /**
     * 按场地查询商品
     */
    @PostMapping("/findMerchantStore")
    public ServerResponse findMerchantStore(@RequestBody CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityDto.setUserInfo(userInfo);
        return commodityService.findMerchantStore(commodityDto);
    }

    /**
     * 商品保存  插入/修改
     */
    @PostMapping("/saveProduct")
    public ServerResponse saveProduct(@RequestBody CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (userInfo.getRoleId().compareTo(RoleEnum.SUB_ACCOUNT.getRoleId()) == 0) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }
        commodityDto.setUserInfo(userInfo);
        return commodityService.saveProduct(commodityDto);
    }

    /**
     * 按分组 查询商品
     */
    @PostMapping("/findByGroup")
    public ServerResponse findByGroup(@RequestBody CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityDto.setUserInfo(userInfo);
        commodityDto.setUserId(userInfo.getId());
        return commodityService.findByGroup(commodityDto);
    }


    /**
     * 根据商品id查询商品 商品分组 商品上架门店
     */
    @PostMapping("/findByIdCommodityVo")
    public ServerResponse findByIdCommodityVo(@RequestBody CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityDto.setUserInfo(userInfo);
        commodityDto.setUserId(userInfo.getId());
        return commodityService.findByIdCommodityVo(commodityDto);
    }

    @PostMapping("/calculateExpirationDays")
    public ServerResponse calculateExpirationDays(@RequestBody CommodityDto commodityDto) {
        return ServerResponse.createBySuccess(commodityService.calculateExpirationDays(commodityDto));
    }

    @GetMapping("/findAllCommodityByUserId")
    public ServerResponse findAllGoodsByUserId(CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);

        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }

        List<CommodityVo> commodityVoList = commodityService.findAllCommodity(commodityDto);
        return ServerResponse.createBySuccess(commodityVoList);
    }

    @GetMapping("/findAllCommodityPage")
    public ServerResponse findAllGoodsByConditionAndPage(CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);

        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }

        return commodityService.findAllCommodityPage(commodityDto);
    }


    @PostMapping("/editVendingMachineGoods")
    public ServerResponse editVendingMachineGoods(HttpServletRequest request,@RequestBody CommodityDto commodityDto){
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }
        return commodityService.editVendingMachineGoods(commodityDto);
    }

    @GetMapping("/findCommodityById")
    public ServerResponse findCommodityById(CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }
        return commodityService.findCommodityById(commodityDto);
    }

    /**
     * 根据条形码和userId查询商品信息
     */
    @GetMapping("/findCommodityByBarCodeAndUserId")
    public ServerResponse findCommodityByBarCodeAndUserId(String brCode, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return ServerResponse.createBySuccess(commodityService.findCommodityByBarCodeAndUserId(brCode,userInfo.getId()));
    }

    @GetMapping("/findCommodityList")
    public ServerResponse findCommodityList(CommodityDto commodityDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityDto.setUserInfo(userInfo);
        return commodityService.findCommodityList(commodityDto);
    }
    @GetMapping("/commodityOffTheShelves")
    public ServerResponse commodityOffTheShelves(CommodityDto commodityDto,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityDto.setUserId(userInfo.getParentId());
        } else {
            commodityDto.setUserId(userInfo.getId());
        }
        commodityService.commodityOffTheShelves(commodityDto);
        return ServerResponse.createBySuccess();
    }
}
